//
//  MyOrderViewController.m
//  LiquorChain
//
//  Created by apple on 2019/1/7.
//  Copyright © 2019年 王冬. All rights reserved.
//

#import "MyOrderViewController.h"

#import "OrderHeader.h"
#import "AllOrderViewController.h"
#import "WaitingPayController.h"
#import "WaitingDeliveryController.h"
#import "WaitingReceiveController.h"
#import "OrderFinshController.h"

@interface MyOrderViewController ()<UIScrollViewDelegate>
{
    UIScrollView *_scrollView;
    OrderHeader *_headView;
    BOOL _isup;
}
@end

@implementation MyOrderViewController
static NSString *CellIdentifier = @"MyOrderCell";
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的订单";
    self.view.backgroundColor = BACKGROUNDCOLOR;
   
     [self createUI];
}

#pragma mark
-(void)changeScrollview:(NSInteger)index{
    
    [UIView animateWithDuration:0 animations:^{
        self->_scrollView.contentOffset = CGPointMake(index *_scrollView.bounds.size.width, 0);
    }];
    
}
-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    if (self.index) {
        
        [self changeScrollview:self.index];
        
        //UIScrollViewDecelerationRateFast;
    }
    
}

-(void)createUI{

    //消除强引用
    __weak typeof(self) weakSelf = self;
    _headView = [[OrderHeader alloc]initWithFrame:CGRectMake(0, kNavBarHeight, SCREEN_WIDTH, 40)];
    _headView.selectIndex = _index;
    _headView.backgroundColor =[UIColor whiteColor];
    _headView.items = @[@"全部",@"待付款",@"待发货",@"待收货",@"已完成"];
    
    
    _headView.itemClickAtIndex = ^(NSInteger index){
        
        [weakSelf adjustScrollView:index];
    };
    [self.view addSubview:_headView];
    
    
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, kNavBarHeight + 40,SCREEN_WIDTH,SCREEN_HEIGHT-kNavBarHeight-40-kIPhoneXBottomHeight)];
    _scrollView.contentSize = CGSizeMake(_scrollView.bounds.size.width*5, _scrollView.bounds.size.height);
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    _scrollView.directionalLockEnabled = YES;
    [self.view addSubview:_scrollView];
    
    [self addViewControllsToScrollView];
    
}

#pragma mark-将4个controller添加到applecontroller上
-(void)addViewControllsToScrollView
{
    AllOrderViewController * allvc = [[AllOrderViewController alloc]init];
    allvc.view.backgroundColor = BACKGROUNDCOLOR;
    allvc.view.frame = CGRectMake(0, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height);
    [_scrollView addSubview:allvc.view];
    [self addChildViewController:allvc];
    
    WaitingPayController * daifkvc = [[WaitingPayController alloc]init];
    daifkvc.view.frame = CGRectMake(_scrollView.bounds.size.width, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height);
    daifkvc.view.backgroundColor = BACKGROUNDCOLOR;
    [_scrollView addSubview:daifkvc.view];
    [self addChildViewController:daifkvc];
    
    WaitingDeliveryController * daifhvc = [[WaitingDeliveryController alloc]init];
    
    daifhvc.view.frame = CGRectMake(_scrollView.bounds.size.width*2, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height);
    daifhvc.view.backgroundColor = BACKGROUNDCOLOR;
    [_scrollView addSubview:daifhvc.view];
    [self addChildViewController:daifhvc];
    
    WaitingReceiveController * daishvc = [[WaitingReceiveController alloc]init];
    daishvc.view.frame = CGRectMake(_scrollView.bounds.size.width*3, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height);
    daishvc.view.backgroundColor = BACKGROUNDCOLOR;
    [_scrollView addSubview:daishvc.view];
    [self addChildViewController:daishvc];
    
    OrderFinshController * orderfinsh = [[OrderFinshController alloc]init];
    orderfinsh.view.frame = CGRectMake(_scrollView.bounds.size.width*4, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height);
    orderfinsh.view.backgroundColor = BACKGROUNDCOLOR;
    [_scrollView addSubview:orderfinsh.view];
    [self addChildViewController:orderfinsh];
    
}
#pragma mark-通过点击button来改变scrollview的偏移量
-(void)adjustScrollView:(NSInteger)index
{
    
    
    [UIView animateWithDuration:0.2 animations:^{
        self->_scrollView.contentOffset = CGPointMake(index *_scrollView.bounds.size.width, 0);
    }];
    
    
}
#pragma mark-选中scorllview来调整headvie的选中
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x/scrollView.bounds.size.width;
    [_headView setSelectAtIndex:index];
    
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
